package cn.iocoder.yudao.module.game.controller.admin.gameToken;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.game.controller.admin.gameToken.vo.*;
import cn.iocoder.yudao.module.game.dal.dataobject.gameToken.GameTokenDO;
import cn.iocoder.yudao.module.game.service.gameToken.GameTokenService;

@Tag(name = "管理后台 - 游戏令牌")
@RestController
@RequestMapping("/game/token")
@Validated
public class GameTokenController {

    @Resource
    private GameTokenService tokenService;

    @PostMapping("/create")
    @Operation(summary = "创建游戏令牌")
    @PreAuthorize("@ss.hasPermission('game:token:create')")
    public CommonResult<Long> createToken(@Valid @RequestBody GameTokenSaveReqVO createReqVO) {
        return success(tokenService.createToken(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新游戏令牌")
    @PreAuthorize("@ss.hasPermission('game:token:update')")
    public CommonResult<Boolean> updateToken(@Valid @RequestBody GameTokenSaveReqVO updateReqVO) {
        tokenService.updateToken(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除游戏令牌")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('game:token:delete')")
    public CommonResult<Boolean> deleteToken(@RequestParam("id") Long id) {
        tokenService.deleteToken(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得游戏令牌")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('game:token:query')")
    public CommonResult<GameTokenRespVO> getToken(@RequestParam("id") Long id) {
        GameTokenDO token = tokenService.getToken(id);
        return success(BeanUtils.toBean(token, GameTokenRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得游戏令牌分页")
    @PreAuthorize("@ss.hasPermission('game:token:query')")
    public CommonResult<PageResult<GameTokenRespVO>> getTokenPage(@Valid GameTokenPageReqVO pageReqVO) {
        PageResult<GameTokenDO> pageResult = tokenService.getTokenPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, GameTokenRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出游戏令牌 Excel")
    @PreAuthorize("@ss.hasPermission('game:token:export')")
    @OperateLog(type = EXPORT)
    public void exportTokenExcel(@Valid GameTokenPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<GameTokenDO> list = tokenService.getTokenPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "游戏令牌.xls", "数据", GameTokenRespVO.class,
                        BeanUtils.toBean(list, GameTokenRespVO.class));
    }

}